package gold.digger;

import java.util.Arrays;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC338 {

    //    public static int []count=new int[Integer.MAX_VALUE+1];
    public static int[] count = new int[100000];

    static {
        count[0] = 0;
        count[1] = 1;
        count[2] = 1;
    }

    public int[] countBits(int num) {
        if (num <= 2 || count[num] > 0) {
            return Arrays.copyOf(count, num + 1);
        }

        int[] res = new int[num + 1];
        for (int i = 1; i <= num; i++) {
            if (count[i] > 0) {
                res[i] = count[i];
            } else {
                int numCount = getSingleBit(i);
                res[i] = numCount;
                count[i] = numCount;
            }
        }

        return res;
    }

    public int getSingleBit(int num) {
        int bitCount = 0;
        int base = (int) Math.pow(2, (int) (Math.log(num) / Math.log(2)));
        while (base >= 1) {
            bitCount += 1;
            num -= base;
            base = (int) Math.pow(2, (int) (Math.log(num) / Math.log(2)));
        }

        return bitCount;
    }


    public static void main(String[] args) {
        LC338 an = new LC338();

        System.out.println(an.countBits(2));
    }

}
